package com.xht.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xht.constants.SystemConstants;
import com.xht.domain.ResponseResult;
import com.xht.domain.entity.Comment;
import com.xht.domain.entity.User;
import com.xht.enums.AppHttpCodeEnum;
import com.xht.exception.SystemExceptin;
import com.xht.mapper.CommentMapper;
import com.xht.service.CommentService;
import com.xht.service.UserService;
import com.xht.utils.BeanCopyUtils;
import com.xht.vo.CommentVo;
import com.xht.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author makejava
 * @since 2025-02-27 21:28:34
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private UserService userService;

    @Autowired
    private CommentMapper commentMapper;


    @Override
    public ResponseResult commentList(String commentType, Long articleId, Integer pageNum, Integer pageSize) {
        //查询对应文章的根评论

        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<Comment>();

        //利用articleId进行判断
        wrapper.eq(SystemConstants.COMMENT_TYPE_ARTICLE.equals(commentType), Comment::getArticleId, articleId);
//        if(articleId != null) {
//            wrapper.eq(Comment::getArticleId, articleId);
//        }

        //判断为 根评论 rootId = -1
        wrapper.eq(Comment::getRootId, SystemConstants.COMMENT_ROOT_STATUS);

        //分类为文章评论 type = 0 (友联评论 type= 1)
        wrapper.eq(Comment::getType, commentType);

        //分页查询
        Page<Comment> page = new Page<>(pageNum, pageSize);
        page(page, wrapper);

        List<CommentVo> commentVoList = toCommentVoList(page.getRecords());

        //添加子评论
        for (CommentVo commentVo : commentVoList) {
            //查询子评论
            List<CommentVo> children = getChildren(commentVo.getId());
            //赋值
            commentVo.setChildren(children);
        }

        return ResponseResult.okResult(new PageVo(commentVoList, page.getTotal()));
    }

    /**
     * 添加 评论
     *
     * @param comment
     * @return
     */
    @Override
    public ResponseResult addComment(Comment comment) {
        //利用 token 添加 评论创建人
        //利用 /mybatisplus/MyMetaObjectHandler 下的类自动 插入
        //评论内容不能为空
        if (!StringUtils.hasText(comment.getContent())) {
            throw new SystemExceptin(AppHttpCodeEnum.CONTEXT_NOT_NULL);
        }
        //TODO 实现 需要登录状态才能评论

        save(comment);
        return ResponseResult.okResult();
    }

    //定义传入子评论方法
    private List<CommentVo> getChildren(Long id) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<Comment>();
        queryWrapper.eq(Comment::getRootId, id);
        queryWrapper.orderByAsc(Comment::getCreateTime);
        List<Comment> list = list(queryWrapper);
        List<CommentVo> commentVoList = toCommentVoList(list);
        return commentVoList;
    }


    //定义方法来 查对应的username(展示用户的昵称) 和 向谁评论的toCommentUsername
    //根据用户id添加对应的头像
    private List<CommentVo> toCommentVoList(List<Comment> list) {
        List<CommentVo> commentVos = BeanCopyUtils.copyBeanList(list, CommentVo.class);

        //遍历Vo集合,将 username 和 toCommentUsername
        for (CommentVo commentVo : commentVos) {
            //查user表,将查到的信息中的nickname 和 avatar 添加到commentVo中
            User user = userService.getById(commentVo.getCreateBy());
            String nickName = user.getNickName();
            String avatar = user.getAvatar();
            commentVo.setUsername(nickName);
            commentVo.setAvatar(avatar);
            //查user表,
            Long toCommentUserId = commentVo.getToCommentUserId();
            //只有ToCommentUserId 不为 -1 时候,才有 TocommentUsername
            if (toCommentUserId != SystemConstants.COMMENT_ROOT_STATUS) {
                String tocommentUsername = userService.getById(toCommentUserId).getNickName();
                commentVo.setToCommentUserName(tocommentUsername);
            }

        }
        return commentVos;
    }
}

